using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class LoaiTinhTrangDAO
    {
        private LoaiTinhTrangDTO[] ConvertToLoaiTinhTrangDTO(DataTable dt)
        {
            int n = dt.Rows.Count;
            LoaiTinhTrangDTO[] Arr = new LoaiTinhTrangDTO[n];
            for (int i = 0; i < n; i++)
            {
                Arr[i] = new LoaiTinhTrangDTO();
                Arr[i].MaTT = Convert.ToInt32(dt.Rows[i]["MaTinhTrang"].ToString());
                Arr[i].TenTT = dt.Rows[i]["TenTinhTrang"].ToString();
            }
            return Arr;
        }

        public LoaiTinhTrangDTO[] LayDanhSachLoaiTinhTrang()
        {
            LoaiTinhTrangDTO[] Arr = new LoaiTinhTrangDTO[100];
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                DataTable dt=new DataTable();
                con = dp.ConnectionData();
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_LayTinhTrangPhong";
                cmd.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                Arr = ConvertToLoaiTinhTrangDTO(dt);
                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return Arr;
        }
    }
}
